dom.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from __future__ import absolute_import, division, unicode_literals
  2. from xml.dom import Node
  3. from . import base
  4. class TreeWalker(base.NonRecursiveTreeWalker):
  5. def getNodeDetails(self, node):
  6. if node.nodeType == Node.DOCUMENT_TYPE_NODE:
  7. return base.DOCTYPE, node.name, node.publicId, node.systemId
  8. elif node.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE):
  9. return base.TEXT, node.nodeValue
  10. elif node.nodeType == Node.ELEMENT_NODE:
  11. attrs = {}
  12. for attr in list(node.attributes.keys()):
  13. attr = node.getAttributeNode(attr)
  14. if attr.namespaceURI:
  15. attrs[(attr.namespaceURI, attr.localName)] = attr.value
  16. else:
  17. attrs[(None, attr.name)] = attr.value
  18. return (base.ELEMENT, node.namespaceURI, node.nodeName,
  19. attrs, node.hasChildNodes())
  20. elif node.nodeType == Node.COMMENT_NODE:
  21. return base.COMMENT, node.nodeValue
  22. elif node.nodeType in (Node.DOCUMENT_NODE, Node.DOCUMENT_FRAGMENT_NODE):
  23. return (base.DOCUMENT,)
  24. else:
  25. return base.UNKNOWN, node.nodeType
  26. def getFirstChild(self, node):
  27. return node.firstChild
  28. def getNextSibling(self, node):
  29. return node.nextSibling
  30. def getParentNode(self, node):
  31. return node.parentNode